# -*- coding:utf-8 -*-
# @FileName :betting_class.py
# @Created  :2024/9/30
# @Author   :天空之城
import time

from game.betting.betting_base import BettingBase
from game.player import HumanPlayer, BotPlayer


class Betting(BettingBase):
    def __init__(self, small_blind=10, big_blind=20):
        # 初始化基类
        super().__init__(small_blind, big_blind)

    def get_player_action(self, player, highest_bet):
        """
        获取玩家的下注动作。这里你可以实现玩家决策逻辑。
        """
        if isinstance(player, HumanPlayer):
            actions = player.get_action(highest_bet)
            return actions
        if isinstance(player, BotPlayer):
            actions = player.get_action(highest_bet)
            player.execute_action(actions, highest_bet)
            print(f'玩家{player.name}:操作{actions}')
            time.sleep(1.5)
            return actions

    def round(self, players, starting_player):
        """
        进行一轮下注，从指定玩家开始。
        :param players: 玩家列表
        :param starting_player: 开始下注的玩家索引。
        """
        current_player_index = starting_player
        highest_bet = self.current_bet
        players_in_round = [player for player in players if player.has_acted]

        while True:
            player = players[current_player_index]
            if player.has_acted:
                action = self.get_player_action(player, highest_bet)
                print(player.name, '|', player.has_acted, "|", action)

            current_player_index = (current_player_index + 1) % len(players)
            # 检查是否所有玩家都进行了跟注或弃牌
            active_bets = [p.current_bet for p in players if p.has_acted]
            if len(active_bets) == 0:
                break
            if all(bet == highest_bet for bet in active_bets):
                break

    def place_blinds(self, players):
        """
        放置小盲注和大盲注。
        """
        small_blind_player = players[(self.dealer_position + 1) % len(players)]
        big_blind_player = players[(self.dealer_position + 2) % len(players)]

        small_blind_player.bet(self.small_blind)
        big_blind_player.bet(self.big_blind)

        self.pot += self.small_blind + self.big_blind
        self.current_bet = self.big_blind
        print(f'大盲:{big_blind_player.name},小盲:{small_blind_player.name}')
